// ex05.c -- generate 100 number integers in range [1, 10] by decreasing order

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// use seed 37
static int seed = 37;
// use bubbleSort
int *bubbleSort(const int len, const int arr[len]);
// generate random number
int randomInt(const int lb, const int ub);
// set seed
void srnd(int s);

int main(int argc, char *argv[]) {
  srnd(atoi(argv[1]));

  int randArr[100];

  for (int i = 0; i < 100; ++i) {
    randArr[i] = randomInt(1, 10);
  }
  int *sortedArr = bubbleSort(100, randArr);

  for (int i = 0; i < 100; ++i) {
    if ((i + 1) % 8 != 0) {
      printf("%d\t", sortedArr[i]);
    } else {
      printf("%d\n", sortedArr[i]);
    }
  }
  puts("");

  return 0;
}

int *bubbleSort(const int len, const int arr[len]) {
  int *sortedArr = calloc(len, sizeof(int));
  for (int i = 0; i < len; ++i) {
    sortedArr[i] = arr[i];
  }

  for (int i = 0; i < len - 1; ++i) {
    for (int j = 0; j < len - 1 - i; ++j) {
      if (sortedArr[j] < sortedArr[j + 1]) {
        int temp = sortedArr[j];
        sortedArr[j] = sortedArr[j + 1];
        sortedArr[j + 1] = temp;
      }
    }
  }

  return sortedArr;
}

int randomInt(const int lb, const int ub) {
  seed = (seed * 373 + 57632) % 32637;

  return (seed + lb) % ub + 1;
}

void srnd(int s) { seed = s; }
